package com.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

import com.dao.MenuDAO;
import com.db.DataBaseConnection;
import com.dto.MenuDTO;

/**
 * 
 * @author 
 *
 */
public class MenuDAOImpl implements MenuDAO {

	/*
	 * (non-Javadoc)
	 * @see com.dao.MenuDAO#querMenuId(int, int)
	 */
	@Override
	public MenuDTO querMenuId(int id1, int id2) throws Exception {
		MenuDTO menu = null;
		String sql = null;
		DataBaseConnection dbc = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;

		try {
			sql = "select id, menu1, menu2 from menu where id1=? and id2=?";
			dbc = new DataBaseConnection();
			pstmt = dbc.getConnection().prepareStatement(sql);
			pstmt.setInt(1, id1);
			pstmt.setInt(2, id2);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				menu = new MenuDTO();
				menu.setId(rs.getInt("id"));
				menu.setMenu1(rs.getString("menu1"));
				menu.setMenu2(rs.getString("menu2"));
			}
			pstmt.close();
			dbc.close();

		} catch (Exception e) {

			throw new Exception(e);

		} finally {
			if (rs != null) {
				rs = null;
			}
			if (pstmt != null) {
				pstmt = null;
			}
		}
		return menu;
	}

	/*
	 * (non-Javadoc)
	 * @see com.dao.MenuDAO#getSubMenus(int)
	 */
	@Override
	public Vector<MenuDTO> getSubMenus(int id1) throws Exception {
		Vector<MenuDTO> submenus = null;
		MenuDTO menu = null;
		String sql = null;
		DataBaseConnection dbc = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			submenus = new Vector<MenuDTO>();
			sql = "select id, menu1, menu2, id2 from menu where id1=?";
			dbc = new DataBaseConnection();
			pstmt = dbc.getConnection().prepareStatement(sql);
			pstmt.setInt(1, id1);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				menu = new MenuDTO();
				menu.setId(rs.getInt("id"));
				menu.setMenu1(rs.getString("menu1"));
				menu.setMenu2(rs.getString("menu2"));
				menu.setId1(id1);
				menu.setId2(rs.getInt("id2"));
				submenus.add(menu);
			}
			pstmt.close();
			dbc.close();

		} catch (Exception e) {
			throw new Exception(e);
		} finally {
			if (rs != null) {
				rs = null;
			}
			if (pstmt != null) {
				pstmt = null;
			}
		}
		return submenus;
	}

	/*
	 * (non-Javadoc)
	 * @see com.dao.MenuDAO#queryMenu()
	 */
	@Override
	public Vector<MenuDTO> queryMenu() throws Exception {
		Vector<MenuDTO> menus = null;
		MenuDTO menu = null;
		String sql = null;
		DataBaseConnection dbc = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			menus = new Vector<MenuDTO>();
			sql = "select distinct id1, menu1 from menu order by id1";
			dbc = new DataBaseConnection();
			pstmt = dbc.getConnection().prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				menu = new MenuDTO();
				menu.setId1(rs.getInt("id1"));
				menu.setMenu1(rs.getString("menu1"));
				menus.add(menu);
			}
			pstmt.close();
			dbc.close();
		} catch (Exception e) {
			throw new Exception(e);
		} finally {
			if (rs != null) {
				rs = null;
			}
			if (pstmt != null) {
				pstmt = null;
			}
		}
		return menus;
	}
}
